package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.util.PageBean;

import javax.annotation.Resource;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class ItemServiceImpl implements ItemService {
    @Resource
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public PageBean<TbItemCat> getinfo(Integer number, Integer pageSize,Long pid) {
        PageBean<TbItemCat> b = new PageBean<>();
        b.setPageNum(number);
        b.setPageSize(pageSize);
        List<TbItemCat> list = tbItemCatMapper.getinfo(b,pid);
        b.setResult(list);
        List<TbItemCat> clist = tbItemCatMapper.getinfoByPid(pid);
        b.setTotal(clist.size());
        return b;
    }

    @Override
    public String toAdd(Long pid) {
        if (pid==0){
            return getItem(pid).getName();
        }else{
            TbItemCat t = getItem(pid);
            TbItemCat t1 = getItem(t.getParentId());
            return t1.getName()+"  >>   "+t.getName();
        }
    }

    public  TbItemCat getItem(Long pid ){
        return  tbItemCatMapper.selectByPrimaryKey(pid);
    }

    @Override
    public Integer addinfo(TbItemCat t) {
        return tbItemCatMapper.insertSelective(t);
    }

    @Override
    public Integer deleteAll(Long[] ids) {
        Integer i = tbItemCatMapper.deleteAll(ids);
        Integer m = tbItemCatMapper.deleteAllByPid(ids);
        return m;
    }

    @Override
    public TbItemCat toUpdate(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateinfo(TbItemCat t) {
        return tbItemCatMapper.updateByPrimaryKeySelective(t);
    }

    //三级联动查询
    @Override
    public List<TbItemCat> toGet1(Long id) {
        return tbItemCatMapper.getByPid(id);
    }

    @Override
    public TbItemCat toGet2(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbItemCat getById(Long categoryId) {
        return tbItemCatMapper.selectByPrimaryKey(categoryId);
    }
}
